//
//  TopListMusicTableViewController.m
//  WXMusic
//
//  Created by 漫步人生路 on 15/8/31.
//  Copyright (c) 2015年 漫步人生路. All rights reserved.
//

#import "TopListMusicTableViewController.h"
#import "TopListTableViewCell.h"
#import "TopListModel.h"
#import "SongsListTableViewController.h"
@interface TopListMusicTableViewController ()
@property (nonatomic, strong) NSMutableArray *topListArray;
@property (nonatomic, strong) NSMutableArray *songsInfoArray;
@end

@implementation TopListMusicTableViewController
//数据请求与解析
- (void)reloadTopList
{
    [LORequestManger GET:kTopListUrl success:^(id response) {
        NSDictionary *dic = (NSDictionary *)response;
        for (NSDictionary *content in dic[@"content"]) {
            TopListModel *topListModel = [TopListModel shareJsonWithDictionary:content];
            [self.topListArray addObject:topListModel];
            NSArray *contentArray = content[@"content"];
            NSMutableArray *array = [[NSMutableArray alloc]init];
            for (NSDictionary *content in contentArray)
            {
                TopListModel *songsInfoModel = [TopListModel shareJsonWithDictionary:content];
                [array addObject:songsInfoModel];
            }
            [self.songsInfoArray addObject:array];
        }
        
        [self.tableView reloadData];
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"%@",error);
    }];
}
- (NSMutableArray *)songsInfoArray
{
    if (!_songsInfoArray) {
        _songsInfoArray = [[NSMutableArray alloc]init];
    }
    return _songsInfoArray;
}
- (NSMutableArray *)topListArray
{
    if (!_topListArray) {
        _topListArray = [[NSMutableArray alloc]init];
    }
    return _topListArray;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self reloadTopList];
    //注册cell
    [self.tableView registerClass:[TopListTableViewCell class] forCellReuseIdentifier:@"topList"];
    /**
     设置模糊视图
     */
    BlurImageView *blurImageView = [[BlurImageView alloc]initWithFrame:kBounds];
    self.tableView.backgroundView = blurImageView;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    // Return the number of sections.
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    // Return the number of rows in the section.
    return self.topListArray.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 170;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    TopListTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"topList" forIndexPath:indexPath];
    TopListModel *topListModel = [self.topListArray objectAtIndex:indexPath.row];
//    NSMutableArray *songsArray = [self.songsInfoArray objectAtIndex:indexPath.row];
//    TopListModel *songsInfoModel = [[TopListModel alloc]init];
    [cell setValueWithModel:topListModel songsInfoArray:nil songsInfoModel:nil];
    
    return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    TopListModel *topListModel = [[TopListModel alloc]init];
    topListModel = [self.topListArray objectAtIndex:indexPath.row];
    SongsListTableViewController *songsListVC = [[SongsListTableViewController alloc]initWithStyle:UITableViewStylePlain];
    songsListVC.songsListCode = [NSString stringWithFormat:@"%@",topListModel.type];
    songsListVC.songsType = @"排行榜";
    [self.navigationController pushViewController:songsListVC animated:YES];

}

/*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the specified item to be editable.
    return YES;
}
*/

/*
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
    } else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
    }   
}
*/

/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
}
*/

/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the item to be re-orderable.
    return YES;
}
*/

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
